// NetService.h
#pragma once

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>

#include "global.h"
#include "Listenable.h"
#include "EEPROMService.h"
#include "ConfigService.h"
#include "SocketService.h"
#include "WiFiConnectEvent.h"
#include "SocketAcceptTask.h"
#include "EventLoopService.h"


#define CONFIG_AP_SSID   "data_camera_ap"
#define CONFIG_AP_PWD  "hello world"

#define CONFIG_SERVER_HOST  "47.100.187.24"
#define CONFIG_SERVER_PORT  8889

#define WIFI_PORT 80

class WiFiService : public Listenable
{
private:
    String ssid;
    String password;
    WiFiClient client;
	bool configServerStarted = false;
    bool connected;
	bool connecting = false;
    uint8_t apMac[6];
	StaticJsonBuffer<1024> jsonBuffer;
	JsonObject& jsonData = jsonBuffer.createObject();
    AsyncWebServer server = AsyncWebServer(WIFI_PORT);
	SocketAcceptTask task;
private:
    bool tryConnect();
    void startConfigServer();
    void scan();
public:
    WiFiService() {};
	void doCheck();
	bool isConnected();
    void init();
};

extern WiFiService wifiService;

